<template>
  <view>
    <view class="cart-container" v-if="cart.length !== 0">
      <!-- 收货地址 -->
      <shop-receipt-adress></shop-receipt-adress>
      <!-- 购物车标题 -->
      <view class="cart-title">
        <uni-icons type="shop" size="18"></uni-icons>
        <text class="cart-title-text">购物车</text>
      </view>
      <!-- 购物车列表 -->
      <uni-swipe-action>
        <block v-for="(item, i) in cart" :key="i">
          <uni-swipe-action-item :right-options="options" @click="goodsRemove(item)"><goods-item :goods="item"
              :show-radio="true" @radio-change="radioChange" :show-num="true"
              @num-change="numChange"></goods-item></uni-swipe-action-item>
        </block>
      </uni-swipe-action>
      <!-- 结算区域 -->
      <shop-settle></shop-settle>
    </view>
    <!-- 购物车为空时 -->
    <view class="cart-empty" v-else>
      <img src="/static/cart_empty@2x.png" class="empty-img">
      <text class="tip">空空如也~</text>
    </view>
  </view>
</template>

<script>
  import
  badgeMix
  from "@/mixins/tabbar-badge.js"
  import {
    mapState,
    mapGetters,
    mapMutations
  } from "vuex"
  export default {
    mixins: [badgeMix],
    computed: {
      ...mapState("m_cart", ["cart"]),
      ...mapGetters("m_cart", ["total"])
    },
    watch: {
      total() {
        this.setBadge()
      }
    },
    data() {
      return {
        options: [{
          text: "删除",
          style: {
            backgroundColor: '#C00000'
          }
        }]
      }
    },
    methods: {
      ...mapMutations("m_cart", ["updateGoodsState", "updateGoodsCount", "removeGoodsById"]),
      radioChange(e) {
        this.updateGoodsState(e)
      },
      numChange(e) {
        this.updateGoodsCount(e)
      },
      goodsRemove(goods) {
        this.removeGoodsById(goods.goods_id)
      }
    }
  }
</script>

<style lang="scss">
  .cart-container {
    padding-bottom: 50px;

    .cart-title {
      height: 40px;
      display: flex;
      align-items: center;
      padding: 5px;
      border-bottom: 1px solid #efefef;

      .cart-title-text {
        font-size: 14px;
        margin-left: 10px;
      }
    }
  }

  .cart-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 150px;

    .empty-img {
      width: 90px;
      height: 90px;
    }

    .tip {
      font-size: 12px;
      color: gray;
      margin-top: 15px;
    }
  }
</style>